package br.elsa.survey.view.beans.managed.tests;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import br.elsa.survey.domain.questions.CheckboxQuestion;
import br.elsa.survey.domain.questions.Question;
import br.elsa.survey.domain.questions.QuestionsFactory;
import br.elsa.survey.services.QuestionService;
import br.elsa.survey.view.beans.managed.AbstractQuestionCreationView;
import br.elsa.survey.view.messages.ViewMessages;

/**
 * TDD test class for {@link AbstractQuestionCreationView}
 * 
 * @author diego
 * @since 2010-10-03
 */
public class AbstractQuestionCreationViewControllerTest {

	/**
	 * Class under test
	 */
	private AbstractQuestionCreationView viewHandler;

	@Mock
	private QuestionService questionService;

	@Mock
	private CheckboxQuestion newQuestion;

	@Mock
	private QuestionsFactory questionsFactory;

	@Mock
	private ViewMessages viewMessages;

	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);

		this.viewHandler = new AbstractQuestionCreationView(questionsFactory, questionService, viewMessages) {

			@Override
			public void resetViewState() {

			}

			@Override
			public Question getNewQuestion() {
				return newQuestion;
			}
		};
	}

	@Test
	public void createQuestionShouldCallQuestionServiceAndFireAnEventInThisOrder() {

		viewHandler.createQuestion();

		InOrder inOrder = inOrder(questionService, viewMessages);
		inOrder.verify(questionService, times(1)).create(newQuestion);
		inOrder.verify(viewMessages, times(1)).addInfoMessage(anyString());
	}

	@Test
	public void createQuestionShouldResetComponentState() {

		AbstractQuestionCreationView spied = spy(viewHandler);
		spied.createQuestion();

		verify(spied, times(1)).resetViewState();
	}
}
